/**********************************************************************/
/*
   Author: Karan Makkar
   Created: Oct  2023
   Updated: Aug 2025, by Youssef Assarssah
   Description: Images for
   Figure 1:Report Apply/Win in National Surveys, by Date First Applied and
   Survey Wave
   Figure 4: Demographic Match in National Surveys and Admin, by Date First
   Applied and Report Won
   
   Outputs:
   Figure4_sus, Figure4_sak
   Figure1_apply, Figure1_suswin, Figure1_sakwin
   
*/
/**********************************************************************/

/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
  if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

  clear all
  set more off
  set matsize 11000
  graph set window fontface "Times New Roman"

* Log
  cap log close
  global prefix: display %tdCYND td(`c(current_date)')
  log using "$KP_logs/${prefix}_reportwin_demogmatch_by_batchround.txt", text replace

* Image filetype
  local ftype "pdf"

  /*----------------------------------------------------*/
* Section: Run programs and set dataset filepaths
/*----------------------------------------------------*/

* Set filepaths for dataset
  global aug20 "$KP_deid_sakernas/Clean/sak_aug20_deid_clean_merged.dta"
  global feb21 "$KP_deid_sakernas/Clean/sak_feb21_deid_clean_merged.dta"
  global aug21 "$KP_deid_sakernas/Clean/sak_aug21_deid_clean_merged.dta"
  global sep20 "$KP_deid_susenas/Clean/sus_sep20_deid_clean_merged.dta"
  global mar21 "$KP_deid_susenas/Clean/sus_mar21_deid_clean_merged.dta"
  global sep21 "$KP_deid_susenas/Clean/sus_sep21_deid_clean_merged.dta"
  global mar22 "$KP_deid_susenas/Clean/sus_mar22_deid_clean_merged.dta"

/*----------------------------------------------------*/
           /* Section: Prepare Batch Schedule Data */
/*----------------------------------------------------*/

import excel "$KP_deid_admin/Raw/00 Kartu Prakerja Batch Schedule.xlsx", sheet("Batch opening schedule") firstrow clear

replace batch = subinstr(batch, "*", "", .)
destring batch, replace

keep batch announcement Beneficiariesperbatch

drop if mi(batch)

gen batch_treated_39 = batch
gen first_apply_batch = batch

destring Beneficiariesperbatch, replace force
gen cumul_win = sum(Beneficiariesperbatch)

replace cumul_win = cumul_win/1000000
label var cumul_win "Cumulative Prakerja Winners (millions)"

format announcement %tdMon_dd_CCYY

tempfile batch_dates
sa `batch_dates'

/*----------------------------------------------------------------*/
* SAKERNAS Cleaning
/*----------------------------------------------------------------*/

  * Load data and append
  use "${aug20}", clear
  append using "${feb21}"
  append using "${aug21}"

  * Merge in demographics match
  fmerge m:1 anon_id4 sak_round using "$KP_deid_sakernas/Clean/age_gender_educ_match_ids.dta", assert(2 3) keep(3) nogen keepusing(match3_sus)

  ********** Save demog match means **********

  * By Batch Treated
  preserve
  bysort anon_id4 sak_round: keep if _n ==1
  collapse match3_sus, by(batch_treated_39)

  tempfile sak_demog_treatbatch
  sa `sak_demog_treatbatch'
  restore

  * By Batch First Applied
  preserve
  bysort anon_id4 sak_round: keep if _n ==1
  collapse match3_sus, by(first_apply_batch)

  tempfile sak_demog_applybatch
  sa `sak_demog_applybatch'
  restore

  preserve
  bysort anon_id4 sak_round: keep if _n ==1
  collapse match3_sus, by(first_apply_batch report_selected)
  tempfile sak_demog_applybatch_split
  sa `sak_demog_applybatch_split'
  restore

 ********** Save Report Apply Shares **********
 *Apply before survey var
  gen apply_before_survey = 0
  replace apply_before_survey  = 1 if sak_round == 5 & first_apply_batch <=3
  replace apply_before_survey  = 1 if sak_round == 6 & first_apply_batch <=11
  replace apply_before_survey  = 1 if sak_round == 7 & first_apply_batch <=17
  
  * By Batch Treated
  preserve
  keep if apply_before_survey ==1  
  bysort anon_id4 sak_round: keep if _n ==1
  collapse report_applied, by(batch_treated_39 sak_round)

  tempfile sak_reportapply_treatbatch
  sa `sak_reportapply_treatbatch'
  restore

  * By Batch First Applied
  preserve
  keep if apply_before_survey ==1  
  bysort anon_id4 sak_round: keep if _n ==1
  collapse report_applied, by(first_apply_batch sak_round)

  tempfile sak_reportapply_applybatch
  sa `sak_reportapply_applybatch'
  restore

  ********** Save Report Win Shares **********

  *Win before survey var
  gen win_before_survey = .
  replace win_before_survey  = ever_win_3 if sak_round == 5 & inrange(batch, 1, 3)
  replace win_before_survey  = ever_win_11 if sak_round == 6 & inrange(batch, 1, 11)
  replace win_before_survey  = ever_win_17 if sak_round == 7 & inrange(batch, 1, 17)

  *Cash before survey var
  bysort anon_id4: gegen date_incentive = max(date_incentive), replace
  gen cash_before_survey = .
  replace cash_before_survey  = 1 if sak_round == 5 & inrange(batch, 2, 3) & date_incentive <= date("2020/7/31", "YMD")
  replace cash_before_survey  = 1 if sak_round == 6 & inrange(batch, 2, 11) & date_incentive <= date("2021/1/31", "YMD")
  replace cash_before_survey  = 1 if sak_round == 7 & inrange(batch, 2, 17) & date_incentive <= date("2021/7/31", "YMD")

  * By Batch Treated
  preserve 
  keep if win_before_survey == 1
  bysort anon_id4 sak_round: keep if _n ==1
  collapse report_selected, by(batch_treated_39 sak_round)

  tempfile sak_reportwin_treatbatch
  sa `sak_reportwin_treatbatch'
  restore

  * By Batch First Applied
  preserve
  keep if win_before_survey == 1
  bysort anon_id4 sak_round: keep if _n ==1
  collapse report_selected, by(first_apply_batch sak_round)

  tempfile sak_reportwin_applybatch
  sa `sak_reportwin_applybatch'
  restore 

  * By Batch First Applied, Cash Before Survey
  keep if cash_before_survey == 1
  bysort anon_id4 sak_round: keep if _n ==1
  collapse report_selected, by(first_apply_batch sak_round)

  tempfile sak_reportwin_applybatch_cash
  sa `sak_reportwin_applybatch_cash'

/*----------------------------------------------------------------*/
* SUSENAS Cleaning
/*----------------------------------------------------------------*/

  * Load data and append
  use "${sep20}", clear
  append using "${mar21}"
  append using "${sep21}"
  append using "${mar22}"
  
  * Merge in demographics match
  fmerge m:1 anon_id4 sus_round using "$KP_deid_susenas/Clean/age_gender_educ_match_ids.dta", assert(2 3) keep(3) nogen keepusing(match3_sus)

  ********** Save demog match means **********
  
  * By Batch Treated
  preserve
  bysort anon_id4 sus_round: keep if _n ==1
  collapse match3_sus, by(batch_treated_39)

  tempfile sus_demog_treatbatch
  sa `sus_demog_treatbatch'
  restore

  * By Batch First Applied
  preserve
  bysort anon_id4 sus_round: keep if _n ==1
  collapse match3_sus, by(first_apply_batch)

  tempfile sus_demog_applybatch
  sa `sus_demog_applybatch'
  restore

  * By Batch First Applied
  preserve
  bysort anon_id4 sus_round: keep if _n ==1

  cap drop report_selected
  gen report_selected =.
  replace report_selected = get_pk if inlist(sus_round, 5, 8)
  replace report_selected = hh_pk_win if inlist(sus_round, 6, 7)
  
  collapse match3_sus, by(first_apply_batch report_selected)

  tempfile sus_demog_applybatch_split
  sa `sus_demog_applybatch_split'
  restore

  ********** Save report win means **********
  gen win_before_survey = .
  replace win_before_survey  = ever_win_5 if sus_round == 5 & inrange(batch, 1, 5)
  replace win_before_survey  = ever_win_11 if sus_round == 6 & inrange(batch, 1, 11)
  replace win_before_survey  = ever_win_18 if sus_round == 7 & inrange(batch, 1, 18)
  replace win_before_survey  = ever_win_22 if sus_round == 8 & inrange(batch, 1, 22)

  *Cash before survey var
  bysort anon_id4: gegen date_incentive = max(date_incentive), replace
  gen cash_before_survey = 0
  replace cash_before_survey  = 1 if sus_round == 5 & inrange(batch, 2, 5) & date_incentive <= date("2020/8/30", "YMD")
  replace cash_before_survey  = 1 if sus_round == 6 & inrange(batch, 2, 11) & date_incentive <= date("2021/2/28", "YMD")
  replace cash_before_survey  = 1 if sus_round == 7 & inrange(batch, 2, 18) & date_incentive <= date("2021/8/30", "YMD")
  replace cash_before_survey  = 1 if sus_round == 8 & inrange(batch, 2, 22) & date_incentive <= date("2022/2/28", "YMD")

  cap drop report_selected
  gen report_selected =.
  replace report_selected = get_pk if inlist(sus_round, 5, 8)
  replace report_selected = hh_pk_win if inlist(sus_round, 6, 7)

  keep if win_before_survey == 1
  bysort anon_id4 sus_round: keep if _n ==1

  * By Batch Treated
  preserve
  collapse report_selected, by(batch_treated_39 sus_round)
  tempfile sus_reportwin_treatbatch
  sa `sus_reportwin_treatbatch'
  restore

  * By Batch First Applied
  preserve
  collapse report_selected, by(first_apply_batch sus_round)
  tempfile sus_reportwin_applybatch
  sa `sus_reportwin_applybatch'
  restore

  * By Batch First Applied, Cash Before Survey
  keep if cash_before_survey == 1
  collapse report_selected, by(first_apply_batch sus_round)
  tempfile sus_reportwin_applybatch_cash
  sa `sus_reportwin_applybatch_cash'

/*----------------------------------------------------------------*/
* Make Graphs
/*----------------------------------------------------------------*/

/***********************
Demog Match
***********************/

* Split by winner status
foreach x in "sak" "sus" {

  if "`x'" == "sak" local lastbatch = 16
  else local lastbatch = 21
  
  u ``x'_demog_applybatch_split', clear

  lab def win 0 "Report Didn't Win" 1 "Report Won"
  lab val report_selected win

  * Drop if missing time or report win variable
  drop if mi(first_apply_batch, report_selected)

  * Merge in dates
  fmerge m:1 first_apply_batch using `batch_dates', nogen assert(2 3) keep(3)

  *** By Batch Date ****
  xtset report_selected announcement

  * Truncated Y-Axis
  xtline match3_sus if first_apply_batch <=`lastbatch', recast(connected) overlay /// 
  plot1opts(msymbol(Oh)) plot2opts(msymbol(Oh)) ///
  xlab(#6, ang(45) labsize(medlarge)) ylab(0.4(0.1)0.9, labsize(medlarge)) ///
  xtitle("Date First Applied", size(medlarge)) ytitle("Share Matched", size(medlarge))  ///
  legend(order(1 "Report Didn't Win" 2 "Report Won") pos(6) row(1) size(medlarge))

  graph export $KP_output/figures/descriptive/`ftype'/Figure4_`x'.`ftype', as(`ftype') replace
}
/***********************
Report Apply
***********************/

  * Set Misc Locals
  u `sak_reportapply_applybatch', clear

  * Drop if missing timevar
  drop if mi(first_apply_batch)

  * Merge in dates
  fmerge m:1 first_apply_batch using `batch_dates', nogen assert(2 3) keep(3)

  replace cumul_win = . if inrange(sak_round, 5, 6)

  * By Date
  xtset sak_round announcement

  xtline report_applied if first_apply_batch <=16, ///
    overlay recast(connected) ///
    plot1opts(msymbol(Oh)) plot2opts(msymbol(Oh)) plot3opts(msymbol(Oh)) ///
    xlab(#6, ang(45) labsize(medlarge)) ylab(0(0.1)0.5, labsize(medlarge)) ///
    legend(rows(1) label(1 "Aug '20") label(2 "Feb '21") label(3 "Aug '21")) /// 
    legend(ring(1) pos(6) size(medlarge)) ///
    xtitle("Date First Applied", size(medlarge)) ytitle("Share Appliers Report Applied", size(medlarge))

  graph export $KP_output/figures/descriptive/`ftype'/Figure1_apply.`ftype', as(`ftype') replace

/***********************
Report Win
***********************/

foreach x in "sak" "sus" {

  if "`x'" == "sak" {
      local symbol = "plot1opts(msymbol(Oh)) plot2opts(msymbol(Oh)) plot3opts(msymbol(Oh))"
      local legend = "legend(rows(1) label(1 Aug '20) label(2  Feb '21) label(3 Aug '21) size(medlarge))"
      local ylab = "ylab(0(0.1)0.5, labsize(medlarge))"
      local lastbatch = 16
  }
  else {
      local symbol = "plot1opts(msymbol(Oh)) plot2opts(msymbol(Oh)) plot3opts(msymbol(Oh)) plot4opts(msymbol(Oh))"
      local legend = "legend(rows(1) label(1 Sep '20) label(2 Mar '21) label(3 Sep '21) label(4 Mar '22) size(medlarge))"
      local ylab = "ylab(0(0.1)0.5, labsize(medlarge))"
      local lastbatch = 21
  }

  u ``x'_reportwin_applybatch', clear

  * Drop if missing time-var
  drop if mi(first_apply_batch)

  * Merge in dates
  fmerge m:1 first_apply_batch using `batch_dates', nogen assert(2 3) keep(3)

  if "`x'" == "sak"  replace cumul_win =. if inrange(sak_round, 5, 6)
  if "`x'" == "sus"  replace cumul_win =. if inrange(sus_round, 5, 7)

  * By Date
  xtset `x'_round announcement

  xtline report_selected if first_apply_batch <= `lastbatch', ///
  overlay recast(connected) ///
  `symbol' ///
  xlab(#6, ang(45) labsize(medlarge)) `ylab' ///
  `legend' ///
  legend(ring(1) pos(6)) ///
  xtitle("Date First Applied", size(medlarge)) ytitle("Share Winners Report Win", size(medlarge))

  graph export $KP_output/figures/descriptive/`ftype'/Figure1_`x'win.`ftype', as(`ftype') replace
}